package com.kalix.btzjjj.memory.biz;

import com.kalix.btzjjj.memory.api.biz.IStorageStatisticsBeanService;
import com.kalix.btzjjj.memory.api.dao.IStorageStatisticsBeanDao;
import com.kalix.btzjjj.memory.api.model.HttpRequestUtil;
import com.kalix.btzjjj.memory.entities.StorageStatisticsBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import org.json.JSONObject;

import java.text.DecimalFormat;
/**
 *@ Author  ZhaoHang
 * @Date  2019/8/28
 **/
public class StorageStatisticsBeanServiceImpl extends GenericBizServiceImpl<IStorageStatisticsBeanDao, StorageStatisticsBean> implements IStorageStatisticsBeanService {
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.btzjjj.portconf");
    @Override
    public String getValue() {
        DecimalFormat df = new DecimalFormat("0.00");
        //S2050
        //获取S2050剩余存储空间
//        String S2050s="{\"value\":\"122348\",\"timestamp\":\"20190813151535\"}";
//        String S2050s="{\"errorCode\":\"BT-30009\",\"message\":\"collect-failed\",\"body\":\"采集失败\"}";
        String S2050s= HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/FAS/064001fa/metrics/Storage_Space_N_Size");
        String S2050surplus=null;
        String S2050sString=null;
        if(S2050s.substring(2,3).equals("e")){
            S2050surplus="0";
        }else{
            JSONObject S2050sobj =new JSONObject(S2050s);
            S2050sString=S2050sobj.get("value").toString();
            S2050surplus= String.valueOf(df.format(Float.valueOf(S2050sString)/1048576));
        }
        //获取S2050总存储空间
//        String S2050t="{\"value\":\"4745991\",\"timestamp\":\"20190813151512\"}";
//        String S2050t="{\"errorCode\":\"BT-30009\",\"message\":\"collect-failed\",\"body\":\"采集失败\"}";
        String S2050t=HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/FAS/064001fa/metrics/Storage_Space_Size");
        String S2050total=null;
        String S2050used=null;
        if(S2050t.substring(2,3).equals("e")){
            S2050total="0";
        }else{
            JSONObject S2050tobj =new JSONObject(S2050t);
            String S2050tString=S2050tobj.get("value").toString();
            S2050used=String.valueOf(df.format((Float.valueOf(S2050tString)-Float.valueOf(S2050sString))/1048576));
            S2050total=String.valueOf(df.format(Float.valueOf(S2050tString)/1048576));
        }
        //2554
        //获取2554剩余存储空间
//        String O2554s="{\"value\":\"157514\",\"timestamp\":\"20190813151609\"}";
//        String O2554s="{\"errorCode\":\"BT-30009\",\"message\":\"collect-failed\",\"body\":\"采集失败\"}";
        String O2554s=HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/FAS/064001f9/metrics/Storage_Space_N_Size");
        String O2554surplus=null;
        String O2554sString=null;
        if(O2554s.substring(2,3).equals("e")){
            O2554surplus="0";
        }else{JSONObject O2554sobj =new JSONObject(O2554s);
            O2554sString=O2554sobj.get("value").toString();
            O2554surplus= String.valueOf(df.format(Float.valueOf(O2554sString)/1048576));
        }
        //获取2554总存储空间
//        String O2554t="{\"value\":\"78762208\",\"timestamp\":\"20190813151553\"}";
//        String O2554t="{\"errorCode\":\"BT-30009\",\"message\":\"collect-failed\",\"body\":\"采集失败\"}";
        String O2554t=HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/FAS/064001f9/metrics/Storage_Space_Size");
        String O2554total=null;
        String O2554used=null;
        if(O2554t.substring(2,3).equals("e")){
            O2554total="0";
        }else{
            JSONObject O2554tobj =new JSONObject(O2554t);
            String O2554tString=O2554tobj.get("value").toString();
            O2554used= String.valueOf(df.format((Float.valueOf(O2554tString)-Float.valueOf(O2554sString))/1048576));
            O2554total=String.valueOf(df.format(Float.valueOf(O2554tString)/1048576));
        }
        //5500V3
        //获取5500V3剩余存储空间
//        String O5500V3s="{\"value\":\"23843136\",\"timestamp\":\"20190813151642\"}";
        String O5500V3s= HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/OceanStor18800/060001f8/metrics/Storage_Space_N_Size");
        String O5500V3surplus=null;
        String O5500V3sString=null;
        if(O5500V3s.substring(2,3).equals("e")){
            O5500V3surplus="0";
        }else{
            JSONObject O5500V3sobj =new JSONObject(O5500V3s);
            O5500V3sString=O5500V3sobj.get("value").toString();
            O5500V3surplus= String.valueOf(df.format(Float.valueOf(O5500V3sString)/1048576));
        }
        //获取5500V3总存储空间
//        String O5500V3t="{\"value\":\"100663296\",\"timestamp\":\"20190813151626\"}";
        String O5500V3t= HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/OceanStor18800/060001f8/metrics/Storage_Space_Size");
        String O5500Vtotal=null;
        String O5500V3used=null;
        if(O5500V3t.substring(2,3).equals("e")){
            O5500Vtotal="0";
        }else{
            JSONObject O5500V3tobj =new JSONObject(O5500V3t);
            String O5500V3tString=O5500V3tobj.get("value").toString();
            O5500V3used= String.valueOf(df.format((Float.valueOf(O5500V3tString)-Float.valueOf(O5500V3sString))/1048576));
            O5500Vtotal=String.valueOf(df.format(Float.valueOf(O5500V3tString)/1048576));
        }
        //9000
        //获取9000剩余存储空间
//        String O9000s="{\"value\":\"2277126\",\"timestamp\":\"20190813151701\"}";
        String O9000s=HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/OceanStor9000/110005d9/metrics/HwSNASClusterRemainCapacity");
        String O9000surplus=null;
        String O9000sString=null;
        if(O9000s.substring(2,3).equals("e")){
            O9000surplus="0";
        }else{
            JSONObject O9000sobj =new JSONObject(O9000s);
            O9000sString=O9000sobj.get("value").toString();
            O9000surplus= String.valueOf(df.format(Float.valueOf(O9000sString)/1048576));
        }
        //获取9000总存储空间
//        String O9000t="{\"value\":\"3577490\",\"timestamp\":\"20190813151717\"}";
        String O9000t=HttpRequestUtil.getXpath("http://"+wsIp+"/api/v1/mo/OceanStor9000/110005d9/metrics/HwSNASClusterSystemCapacity");
        String O9000used=null;
        String O9000Vtotal=null;
        if(O9000t.substring(2,3).equals("e")){
            O9000Vtotal="0";
        }else{
            JSONObject O9000tobj =new JSONObject(O9000t);
            String O9000tString=O9000tobj.get("value").toString();
            O9000used= String.valueOf(df.format((Float.valueOf(O9000tString)-Float.valueOf(O9000sString))/1048576));
            O9000Vtotal=String.valueOf(df.format(Float.valueOf(O9000tString)/1048576));
        }

        String res="{" +
                "\"title_1\": \"netapp fas s2050\"," +
                "\"title_2\": \"netapp fas 2554\"," +
                "\"title_3\": \"华为 5500v3\"," +
                "\"title_4\": \"华为 9000\"," +
                "\"subtitle_1\": \""+S2050total+"\"," +
                "\"subtitle_2\": \""+O2554total+"\"," +
                "\"subtitle_3\": \""+O5500Vtotal+"\"," +
                "\"subtitle_4\": \""+O9000Vtotal+"\"," +
                "\"data_1\": [" +
                "[" +
                "\"可用空间(TB)\"," +
                "\""+S2050surplus+"\"" +
                "]," +
                "[" +
                "\"已用空间(TB)\"," +
                "\""+S2050used+"\"" +
                "]" +
                "]," +
                "\"data_2\": [" +
                "[" +
                "\"可用空间(TB)\"," +
                "\""+O2554surplus+"\""+
                "]," +
                "[" +
                "\"已用空间(TB)\"," +
                "\""+O2554used+"\"" +
                "]" +
                "]," +
                " \"data_3\": [" +
                "[" +
                "\"可用空间(TB)\"," +
                "\""+O5500V3surplus+"\"" +
                "]," +
                "[" +
                "\"已用空间(TB)\"," +
                "\""+O5500V3used+"\"" +
                "]" +
                "]," +
                "\"data_4\": [" +
                "[" +
                "\"可用空间(PB)\"," +
                "\""+O9000surplus+"\"" +
                "]," +
                "[" +
                "\"已用空间(PB)\"," +
                "\""+O9000used+"\"" +
                "]" +
                "]" +
                "}";
        return res;
    }
}
